<?php
namespace app\api\controller;

use app\api\model\User as UserModel;
use think\facade\Request;

class User
{
    public function register()
    {
        $data = Request::post();
        $user = new UserModel();
        $user->save($data);
        return json(['code' => 200, 'message' => '注册成功']);
    }

    public function login()
    {
        $data = Request::post();
        $user = UserModel::where('username', $data['username'])->find();
        if ($user && password_verify($data['password'], $user->password)) {
            return json(['code' => 200, 'message' => '登录成功', 'data' => $user]);
        }
        return json(['code' => 401, 'message' => '用户名或密码错误']);
    }
}
